use chrono::{DateTime, Utc}; // 数据库时间类型
use serde::Deserialize; // 反序列化
use serde::Serialize; // 序列化
use sqlx::FromRow;
use validator::Validate;

// === com ===
#[derive(FromRow, Debug, Deserialize)]
pub struct ID {
    pub id: u64,
}

#[derive(FromRow, Debug, Deserialize)]
pub struct List {
    pub id: u64,
}

// ****============================== User  =================================****
#[derive(Debug, Serialize, Deserialize, Validate)]
pub struct UserLoginReq {
    #[validate(length(equal = 11))]
    pub username: String,
    #[validate(length(min = 5))]
    pub password: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserEdit {
    pub id: u64,
    pub username: Option<String>,
    pub tel: Option<String>,
    pub remark: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserSearch {
    pub page: u64,
    pub page_size: u64,
    pub id: Option<String>,
    pub username: Option<String>,
    pub tel: Option<String>,
}

#[derive(Deserialize, Debug, Validate)]
pub struct User {
    #[validate(length(min = 3))]
    pub username: String,
    #[validate(length(min = 5))]
    pub password: String,
    #[validate(length(equal = 11))]
    pub tel: String,
    pub remark: Option<String>,
}

#[derive(FromRow, Serialize, Debug)]
pub struct UserInfo {
    id: u64,
    username: String,
    created_at: DateTime<Utc>,
}

// ****============================== Topic  =================================****
#[derive(Debug, Serialize, Deserialize)]
pub struct TopicSearch {
    pub page: u64,
    pub page_size: u64,
    pub id: Option<String>,
    pub name: Option<String>,
    pub r#type: Option<i32>,
    pub topic_text: Option<String>,
    pub message: Option<String>,
}

#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct TopicInfo {
    pub id: Option<u64>,
    pub r#type: i32,
    pub name: String,
    pub topic_text: String,
    pub message: String,
    pub remark: String,
}

// ****============================== Device  =================================****
#[derive(Debug, Serialize, Deserialize)]
pub struct DeviceSearch {
    pub page: u64,
    pub page_size: u64,
    pub id: Option<String>,
    pub name: Option<String>,
    pub model: Option<String>,
    pub device_type: Option<i64>,
}

#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct DeviceInfo {
    pub id: Option<u64>,
    pub name: String,
    pub model: String,
    pub device_type: u32,
    pub power_type: u32,
    pub power_value: Option<String>,
    pub price: f64,
    pub device_img: Option<String>,
    pub remark: Option<String>,
}

// *****============================== Core_control  =================================****
#[derive(Debug, Serialize, Deserialize)]
pub struct CoreControlSearch {
    pub page: u64,
    pub page_size: u64,
    pub id: Option<String>,
    pub name: Option<String>,
    pub model: Option<String>,
}

#[derive(Debug, FromRow, Deserialize)]
pub struct ControlCore {
    pub id: Option<u64>,
    pub name: String,
    pub model: String,
    pub power: String,
    pub price: f64,
    pub core_img: String,
    pub remark: String,
}

// *****============================== Modules  =================================****
#[derive(Debug, Serialize, Deserialize)]
pub struct ModulesSearch {
    pub page: u64,
    pub page_size: u64,
    pub id: Option<String>,
    pub name: Option<String>,
}

#[derive(Debug, FromRow, Deserialize)]
pub struct ModulesInfo {
    pub id: Option<u64>,
    pub name: String,
    pub modules_img: Option<String>,
    pub remark: Option<String>,
    pub task_list: Vec<TaskInfo>,
}

#[derive(Debug, FromRow, Deserialize)]
pub struct ModulesEditDetail {
    pub id: Option<u64>,
    pub name: String,
    pub modules_img: Option<String>,
    pub remark: Option<String>,
}

#[derive(Debug, FromRow, Deserialize)]
pub struct ModulesEdit {
    pub modules_detail: ModulesEditDetail,
    pub task_add: Option<Vec<TaskInfo>>,
    pub task_edit: Option<Vec<TaskInfo>>,
    pub task_delete: Option<Vec<u64>>,
}

// *****============================== Task  =================================****
#[derive(Debug, FromRow, Deserialize)]
pub struct TaskInfo {
    pub id: Option<u64>,
    pub core: u64,
    pub device: u64,
    pub topic: u64,
    pub task_name: String,
    pub gpio: String,
}
